﻿using System;
using System.Drawing;
using System.Threading;
using ScanAPIHelper;

namespace AccessControl.HardwareLibrary.FS80
{
	public class FS80Device
	{
		#region Constant
		const int FTR_ERROR_EMPTY_FRAME = 4306;
		const int FTR_ERROR_MOVABLE_FINGER = 0x20000001;
		const int FTR_ERROR_NO_FRAME = 0x20000002;
		const int FTR_ERROR_USER_CANCELED = 0x20000003;
		const int FTR_ERROR_HARDWARE_INCOMPATIBLE = 0x20000004;
		const int FTR_ERROR_FIRMWARE_INCOMPATIBLE = 0x20000005;
		const int FTR_ERROR_INVALID_AUTHORIZATION_CODE = 0x20000006;

		/* Other return codes are Windows-compatible */
		const int ERROR_NO_MORE_ITEMS = 259;                // ERROR_NO_MORE_ITEMS
		const int ERROR_NOT_ENOUGH_MEMORY = 8;              // ERROR_NOT_ENOUGH_MEMORY
		const int ERROR_NO_SYSTEM_RESOURCES = 1450;         // ERROR_NO_SYSTEM_RESOURCES
		const int ERROR_TIMEOUT = 1460;                     // ERROR_TIMEOUT
		const int ERROR_NOT_READY = 21;                     // ERROR_NOT_READY
		const int ERROR_BAD_CONFIGURATION = 1610;           // ERROR_BAD_CONFIGURATION
		const int ERROR_INVALID_PARAMETER = 87;             // ERROR_INVALID_PARAMETER
		const int ERROR_CALL_NOT_IMPLEMENTED = 120;         // ERROR_CALL_NOT_IMPLEMENTED
		const int ERROR_NOT_SUPPORTED = 50;                 // ERROR_NOT_SUPPORTED
		const int ERROR_WRITE_PROTECT = 19;                 // ERROR_WRITE_PROTECT
		const int ERROR_MESSAGE_EXCEEDS_MAX_SIZE = 4336;    // ERROR_MESSAGE_EXCEEDS_MAX_SIZE
		#endregion

		#region Fields

        private readonly ScanAPIHelper.Device _device;
		private byte[] _frame;

		#endregion

		#region Constructor

		public FS80Device()
		{
			ScanAPIHelper.Device.BaseInterface = 0;
            _device = new ScanAPIHelper.Device();
			try
			{
				_device.Open();
			}
			catch (ScanAPIException exception)
			{
				throw new ApplicationException("ایراد در برقراری ارتباط با اسکنر اثر انگشت");
			}
			
		}

		#endregion

		#region Privarte Methods

		private int GetFrame()
		{
			_frame = null;
			
			var lastErrorCode = 0;
			if ((!_device.IsFingerPresent)||(lastErrorCode != _device.LastErrorCode))
			{
				lastErrorCode = _device.LastErrorCode;
				return lastErrorCode;
			}
			Thread.Sleep(0);
			try
			{
				_frame = _device.GetFrame();
			}
			catch (ScanAPIException)
			{
				_frame = null;
				lastErrorCode = _device.LastErrorCode;
			}
			return lastErrorCode;
		}

		#endregion

		#region Public Method

		public string ReadFinger(out byte[] finger)
		{
			int i;
			i=GetFrame();
			finger = _frame;
			switch (i)
			{
				case 0:
					break;
				case FTR_ERROR_EMPTY_FRAME:
					return ("انگشت را روی اسکنر قرار دهید");
				case FTR_ERROR_NO_FRAME:
				case FTR_ERROR_MOVABLE_FINGER:
					return ("انگشت را به صورت ثابت روی اسکنر قرار دهید");
				case FTR_ERROR_HARDWARE_INCOMPATIBLE:
				case FTR_ERROR_FIRMWARE_INCOMPATIBLE:
					return ("خطا در برقراری ارتباط با اسکنر");
				default:
					return String.Format("خطای ناشناخته #{0}", i);
			}

			return string.Empty;
		}

		#endregion

		#region Public Property

		public Size ImageSize
		{
			get
			{
				return _device.ImageSize;
			}
		}

		#endregion

	}
}
